<template>
    <!-- 碳减排量计算 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生产日期">
        <el-date-picker
          v-model="daterangeDt"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <!-- 数据表格区域 -->
    <div v-loading="loading" class="result-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix" style="text-align: center;">
          <span> 支撑数据汇总</span>
        </div>
        <div class="table-container">
          <div slot="header" class="clearfix" style="text-align:left;margin-bottom: 15px;font-weight: bold;">
            <span> 生产数据</span>
          </div>

          <table class="data-table">
            <tr>
              <td class="label-cell">废胎(吨)</td>
              <td class="value-cell">{{ accountResultData.wasteTireWeight || 0 }}</td>
              <td class="label-cell">毛丝(吨)</td>
              <td class="value-cell">{{ accountResultData.mixedWireWeight || 0 }}</td>
              <td class="label-cell">钢丝(吨)</td>
              <td class="value-cell">{{ accountResultData.steelWireWeight || 0 }}</td>
            </tr>
            <tr>
              <td class="label-cell">口圈(吨)</td>
              <td class="value-cell">{{ accountResultData.tireRimWeight || 0 }}</td>
              <td class="label-cell">胶粉(吨)</td>
              <td class="value-cell">{{ accountResultData.rubberPowderWeight || 0 }}</td>
              <td class="label-cell">基质沥青(吨)</td>
              <td class="value-cell">{{ accountResultData.jzlq || 0 }}</td>
            </tr>
            <tr>
              <td class="label-cell">改性沥青(吨)</td>
              <td class="value-cell">{{ accountResultData.gxlq || 0 }}</td>
              <td class="label-cell">SBS(吨)</td>
              <td class="value-cell">{{ accountResultData.sbs || 0 }}</td>
              <td class="label-cell">界面剂(吨)</td>
              <td class="value-cell">{{ accountResultData.jmj || 0 }}</td>
            </tr>
            <tr>
              <td class="label-cell">改性剂(吨)</td>
              <td class="value-cell">{{ accountResultData.gxj || 0 }}</td>
              <td class="label-cell">硫磺(吨)</td>
              <td class="value-cell">{{ accountResultData.lh || 0 }}</td>
              <td class="label-cell">降粘剂(吨)</td>
              <td class="value-cell">{{ accountResultData.jnj || 0 }}</td>
            </tr>
            <tr>
              <td class="label-cell">稳定剂(吨)</td>
              <td class="value-cell">{{ accountResultData.wdj || 0 }}</td>
              <td class="label-cell">沥青片(吨)</td>
              <td class="value-cell">{{ accountResultData.lqp || 0 }}</td>
              <td class="label-cell">橡胶油(吨)</td>
              <td class="value-cell">{{ accountResultData.xjy || 0 }}</td>
            </tr>
            <tr>
              <td class="label-cell" style="width: 15%;">橡胶沥青(吨)</td>
              <td class="value-cell" colspan="5" style="color: red;font-weight: bold;">{{ accountResultData.asphalt || 0 }}</td>
            </tr>
          </table>

          <div slot="header" class="clearfix" style="text-align:left;margin-bottom: 15px;margin-top: 15px;font-weight: bold;">
            <span> 能耗数据</span>
          </div>
          <table class="data-table">
            <tr>
              <td class="energy-label-cell">电量(kWh)</td>
              <td class="energy-value-cell">{{ accountResultData.ammeterPae || 0 }}</td>
              <td class="energy-label-cell">天然气(Nm3)</td>
              <td class="energy-value-cell">{{ accountResultData.gasVal || 0 }}</td>
            </tr>
            <tr>
              <td class="energy-label-cell">汽油(L)</td>
              <td class="energy-value-cell">{{ accountResultData.gasolineVal || 0 }}</td>
              <td class="energy-label-cell">柴油(L)</td>
              <td class="energy-value-cell">{{ accountResultData.dieselOilVal || 0 }}</td>
            </tr>
            <tr>
              <td class="energy-label-cell" style="width: 15%;">冷喷油(kg)</td>
              <td class="energy-value-cell" colspan="3">{{ accountResultData.coldInjectionVal || 0 }}</td>
            </tr>
          </table>
        </div>
      </el-card>
      <el-card class="box-card" style="margin-top: 20px;">
        <div slot="header" class="clearfix" style="text-align: center;">
          <span> 基准线排放量计算</span>
        </div>
        <el-table :data="baseLineResult" style="width: 100%">
          <el-table-column prop="id" align="center" width="100" class-name="label-cell">
            <template slot-scope="scope">
              <div class="id-tag primary-tag">{{ scope.row.id }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="参数" width="350" class-name="label-cell"></el-table-column>
          <el-table-column prop="unit" label="单位" width="150" class-name="label-cell"></el-table-column>
          <el-table-column prop="resultVal" label="计算结果" width="350" class-name="label-cell">
            <template slot-scope="scope">
              {{ scope.row.resultVal ? scope.row.resultVal.toFixed(3) : 0 }}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注"  class-name="label-cell"></el-table-column>
        </el-table>
      </el-card>
      
      <el-card class="box-card" style="margin-top: 20px;">
        <div slot="header" class="clearfix" style="text-align: center;">
          <span> 项目排放量及减排量</span>
        </div>
        <el-table :data="emissionResult" style="width: 100%">
          <el-table-column prop="id" align="center" width="100" class-name="label-cell">
            <template slot-scope="scope">
              <div class="id-tag success-tag">{{ scope.row.id }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="参数" width="350" class-name="label-cell"></el-table-column>
          <el-table-column prop="unit" label="单位" width="150" class-name="label-cell"></el-table-column>
          <el-table-column prop="resultVal" label="计算结果" width="350" class-name="label-cell">
            <template slot-scope="scope">
              {{ scope.row.resultVal ? scope.row.resultVal.toFixed(3) : 0 }}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" class-name="label-cell"></el-table-column>
        </el-table>
      </el-card>
      
      <!-- 添加减排量计算结果 -->
      <el-card class="box-card" style="margin-top: 20px;" v-if="baseLineResult.length > 0 && emissionResult.length > 0">
        <div class="emission-reduction-result">
          <div class="result-text-container">
            <p>
              根据以上计算过程，基准线排放量为 
              <span class="number-tag danger-tag">{{ getBaseLineEmission }}</span> tCO2e ，
              项目生产排放量为 <span class="number-tag warning-tag">{{ getProjectEmission }}</span> tCO2e, 
              可实现碳减排量 <span class="number-tag success-tag">{{ getEmissionReduction }}</span> tCO2e.
            </p>
            <p>
              另外，如果不按上述过程分解计算，根据IPCC固体废弃物处理行业碳排放量平均值计算,
              每处理1万吨固体废弃物，可减排1.12万吨CO2,那么可减排 
              <span class="number-tag success-tag">{{ getIPCCEmissionReduction }}</span> tCO2e.
            </p>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { listAccountResult } from "@/api/mes/mod/accountResult";
export default {
    name: 'AccountResult',
    data() {
        return {
            // 遮罩层
            loading: false,
            // 显示搜索条件
            showSearch: true,
            // 日期范围
            daterangeDt: [],
            // 查询参数
            queryParams: {
                timeInterval: {
                    beginDt: undefined,
                    endDt: undefined
                }
            },
            // 计算结果数据
            accountResultData: null,
            // 基准线排放量计算数据
            baseLineResult: [],
            // 项目排放量及减排量
            emissionResult: []
        }
    },
    created() {
        // 组件创建时可以初始化数据
        this.getList();
    },
    computed: {
        // 计算减排量
        getEmissionReduction() {
            // 找到基准线总CO2排放（ID为F的数据）
            const baseLineTotal = this.baseLineResult.find(item => item.id === 'F');
            // 找到项目总CO2排放（ID为J的数据）
            const emissionTotal = this.emissionResult.find(item => item.id === 'J');
            
            if (baseLineTotal && baseLineTotal.resultVal !== null && emissionTotal && emissionTotal.resultVal !== null) {
                // 计算减排量 = 基准线总排放 - 项目总排放
                const reduction = baseLineTotal.resultVal - emissionTotal.resultVal;
                // 返回保留三位小数的结果
                return reduction.toFixed(3);
            }
            
            return '0.000';
        },
        // 获取基准线排放量
        getBaseLineEmission() {
            // 找到基准线总CO2排放（ID为F的数据）
            const baseLineTotal = this.baseLineResult.find(item => item.id === 'F');
            
            if (baseLineTotal && baseLineTotal.resultVal !== null) {
                return baseLineTotal.resultVal.toFixed(3);
            }
            
            return '0.000';
        },
        // 获取项目排放量
        getProjectEmission() {
            // 找到项目总CO2排放（ID为J的数据）
            const emissionTotal = this.emissionResult.find(item => item.id === 'J');
            
            if (emissionTotal && emissionTotal.resultVal !== null) {
                return emissionTotal.resultVal.toFixed(3);
            }
            
            return '0.000';
        },
        // 获取IPCC计算方法的减排量
        getIPCCEmissionReduction() {
            // 获取废胎处理量
            const wasteTireWeight = this.accountResultData && this.accountResultData.wasteTireWeight ? this.accountResultData.wasteTireWeight : 0;
            // 根据IPCC固体废弃物处理行业碳排放量计算: 每处理1万吨固体废弃物，可减排1.12万吨CO2
            // 计算公式: 废胎重量(吨) * 1.12 / 10000
            const reductionByIPCC = wasteTireWeight * 1.12 / 10;
            
            return reductionByIPCC.toFixed(3);
        }
    },
    methods: {
        /** 查询账目结果列表 */
        getList() {
            this.loading = true;
            // 处理日期范围
            if (this.daterangeDt && this.daterangeDt.length > 0) {
                this.queryParams.timeInterval.beginDt = this.daterangeDt[0];
                this.queryParams.timeInterval.endDt = this.daterangeDt[1];
            } else {
                this.queryParams.timeInterval.beginDt = undefined;
                this.queryParams.timeInterval.endDt = undefined;
            }
            
            // 调用搜索API
            this.listAccountResult();
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.daterangeDt = [];
            this.resetForm("queryForm");
            this.queryParams = {
                timeInterval: {
                    beginDt: undefined,
                    endDt: undefined
                }
            };
            this.handleQuery();
        },
        /** 调用后端接口获取数据 */
        listAccountResult() {
            this.loading = true;
            // 构建请求参数
            const params = {
                'beginDt': this.queryParams.timeInterval.beginDt,
                'endDt': this.queryParams.timeInterval.endDt
            };
            
            // 这里调用您的API接口，例如:
            listAccountResult(params).then(response => {
                // 检查响应代码，处理错误情况
                if (response.code === 500) {
                    this.$message.error(response.msg || "当前时间该区域无数据，请重新选择！");
                    // 将所有数据字段赋值为0，而不是设为null
                    this.accountResultData = {
                        wasteTireWeight: 0,
                        mixedWireWeight: 0,
                        steelWireWeight: 0,
                        tireRimWeight: 0,
                        rubberPowderWeight: 0,
                        jzlq: 0,
                        gxlq: 0,
                        sbs: 0,
                        jmj: 0,
                        gxj: 0,
                        lh: 0,
                        jnj: 0,
                        wdj: 0,
                        lqp: 0,
                        xjy: 0,
                        asphalt: 0
                    };
                    this.baseLineResult = [];
                    this.emissionResult = [];
                } else {
                    // 设置结果数据
                    if (response.data) {
                        if (response.data.modAccountBaseData) {
                            this.accountResultData = response.data.modAccountBaseData;
                        } else {
                            this.accountResultData = null;
                        }
                        
                        // 设置基准线排放量计算数据
                        this.baseLineResult = response.data.baseLineResult || [];
                        // 设置项目排放量及减排量数据
                        this.emissionResult = response.data.emissionResult || [];
                    } else {
                        this.accountResultData = null;
                        this.baseLineResult = [];
                        this.emissionResult = [];
                    }
                }
                this.loading = false;
            }).catch(error => {
                console.error("获取账目结果失败", error);
                // 在错误情况下将所有数据字段赋值为0
                this.accountResultData = {
                    wasteTireWeight: 0,
                    mixedWireWeight: 0,
                    steelWireWeight: 0,
                    tireRimWeight: 0,
                    rubberPowderWeight: 0,
                    jzlq: 0,
                    gxlq: 0,
                    sbs: 0,
                    jmj: 0,
                    gxj: 0,
                    lh: 0,
                    jnj: 0,
                    wdj: 0,
                    lqp: 0,
                    xjy: 0,
                    asphalt: 0
                };
                this.baseLineResult = [];
                this.emissionResult = [];
                this.loading = false;
            });
            
        }
    }
}
</script>

<style scoped>
.result-container {
  margin-top: 15px;
}

.table-container {
  width: 100%;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  background-color: #f5f7fa;
}

.data-table td {
  padding: 10px;
  border: 1px solid #dfe6ec;
}

.label-cell {
  width: 15%;
  text-align: left;
  font-weight: bold;
}

.value-cell {
  width: 10%;
  text-align: left;
  background-color: #fff;
}

.energy-label-cell {
  width: 10%;
  text-align: left;
  font-weight: bold;
}

.energy-value-cell {
  width: 10%;
  text-align: left;
  background-color: #fff;
}

/* 序号ID标签样式 */
.id-tag {
  display: inline-block;
  width: 32px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  border-radius: 4px;
  color: white;
  font-weight: normal;
}

.primary-tag {
  background-color: #409EFF;
}

.success-tag {
  background-color: #67C23A;
}

/* 设置表格标题行样式 */
::v-deep .el-table__header-wrapper th {
  background-color: #f5f7fa !important;
  color: #606266 !important;
  font-weight: bold !important;
}

/* 数字标签样式 */
.result-text-container {
  padding: 20px;
  line-height: 2;
  font-size: 14px;
}

.number-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  color: white;
  font-weight: bold;
}

.danger-tag {
  background-color: #F56C6C;
}

.warning-tag {
  background-color: #E6A23C;
}

.success-tag {
  background-color: #67C23A;
}
</style>